#!/bin/bash
# Start the Anaconda's Python module $1.
#
# Examples:
#   ./start-module pyanaconda.modules.boss
#   ./start-module --env LD_PRELOAD=libgomp.so.1 pyanaconda.modules.payloads
#

# Process the arguments.
while true
do
  case $1 in
    # Set up the environment.
    --env)
      export $2
      shift 2
    ;;
    # Nothing else to do.
    *)
      break
    ;;
  esac
done

# Add Anaconda addons to the PYTHONPATH.
PYTHONPATH="$PYTHONPATH:/usr/share/anaconda/addons"

# Export the modified PYTHONPATH.
export PYTHONPATH

# Start a Python module in the detached mode.
python3 -m $1 &
module_pid="$!"

# Wait for a minute in the detached mode.
sleep 60 &
timeout_pid="$!"

# If the Python module fails before the timeout, return its exit status.
# Otherwise, return 0.
wait -n "${timeout_pid}" "${module_pid}"
